﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace DAL
{
    public class Tbl_quydinhloaisp_DAL
    {
        public SqlConnection cn = null;
        public Tbl_quydinhloaisp_DAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }

        public Tbl_quydinhloaisp_DAL(Tbl_quydinhloaisp_DAL qd)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }

        public Tbl_quydinhloaisp_Collection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("GET_QUYDINHLOAISP", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            Tbl_quydinhloaisp_Collection qdCollection = new Tbl_quydinhloaisp_Collection();

            while (dr.Read())
            {
                Tbl_quydinhloaisp_Entities qd = new Tbl_quydinhloaisp_Entities();
                qd.STT = int.Parse(dr.GetValue(0).ToString());
                qd.STT_Loai = int.Parse(dr.GetValue(1).ToString());
                qd.MaQuyDinh = int.Parse(dr.GetValue(2).ToString());
                qd.NoiDung = dr.GetValue(3).ToString();
                qdCollection.Add(qd);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return qdCollection;
        }
        public void insert(Tbl_quydinhloaisp_Entities qd)
        {
            SqlCommand cm = new SqlCommand("INSERT_QUYDINHLOAISP", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@STT_Loai", qd.STT_Loai);
            cm.Parameters.AddWithValue("@MaQuyDinh", qd.MaQuyDinh);
            cm.Parameters.AddWithValue("@NoiDung", qd.NoiDung);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("DELETE_QUYDINHLOAISP", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@STT", iD);
            cm.ExecuteNonQuery();
        }
        public void Update(Tbl_quydinhloaisp_Entities qd)
        {
            SqlCommand cm = new SqlCommand("UPDATE_QUYDINHLOAISP", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@STT_Loai", qd.STT_Loai);
            cm.Parameters.AddWithValue("@MaQuyDinh", qd.MaQuyDinh);
            cm.Parameters.AddWithValue("@NoiDung", qd.NoiDung);
            cm.ExecuteNonQuery();
        }
    }
}
